<?php
/**
 * Class Db_Cms_GeneralSkinFiles created by Havriniuc Serghei
 * Date: 07.08.12
 * Time: 10:01
 */

class Db_Cms_GeneralSkinFiles extends Common {
	protected static $tableName = TAB_GENERAL_SKIN_FILES;
	protected static $dbFields = array(
		'id',
		'gsf_file_name',
		'gsf_file_type',
		'gsf_special_content',
		'gsf_media',
		'gsf_position',
		'deleted'
	);
    public static $positionField = 'gsf_position';
    /**
     * @var Db_Cms_GeneralSkinFiles
     */
    public $objectArray;

	public $id;
	public $gsf_file_name;
	public $gsf_file_type;
	public $gsf_special_content;
	public $gsf_media;
	public $gsf_position;
	public $deleted;

	function __construct($value=NULL, $fieldName=NULL) {
		parent::__construct($value, $fieldName);
	}

	public static function getCurrentGeneralSkinFiles() {
		$sql = "SELECT `gsf`.`gsf_file_name`, `gsf`.`gsf_file_type`, `gsf`.`gsf_special_content`, `gsf`.`gsf_media` FROM ?f AS `gsf`
				INNER JOIN ?f AS `gsfp` ON `gsfp`.`gsfp_file_id` = `gsf`.`id`
				WHERE `gsfp`.`gsfp_project_id` = ?i AND `gsf`.`deleted` = 0
				ORDER BY `gsf`.`gsf_position` ASC;";
		$result = self::$DB->q($sql, self::$tableName, TAB_GENERAL_SKIN_FILE_PROJECTS, self::$PROJECT['id']);
		$generalFiles = array();
		while ($data = $result->fetch_assoc()) {
			$generalFiles[$data['gsf_file_type']][] = array(
				'name' => $data['gsf_file_name'],
				'special_content' => $data['gsf_special_content'],
				'media' => $data['gsf_media']
			);
		}
		return $generalFiles;
	}

    /**
     * @static
     * @return array
     * This function gives all All General Files
     */
	public static function getAllGeneralFiles(){
		$sql = "SELECT `gsf`.*,  `gsf`.`id` AS `gsf_id`, `pr`.`id` as `project_id`, `c`.*, GROUP_CONCAT(`c`.`c_identifier`) AS `country_names`, GROUP_CONCAT(`pr`.`pr_name`) AS `project_names`, IF(`gsf`.`id` = `gsfp`.`gsfp_file_id`, 1, 0) AS `is_used` FROM ?f AS `gsf`
        		LEFT JOIN ?f AS `gsfp` ON `gsf`.`id` = `gsfp`.`gsfp_file_id`
        		LEFT JOIN ?f AS `pr` ON `pr`.`id` = `gsfp`.`gsfp_project_id`
        		LEFT JOIN ?f AS `c` ON `c`.`id` = `pr`.`pr_country_id`
        		WHERE `gsf`.`deleted` = 0 GROUP BY `gsf`.`id` ORDER BY `gsf`.`gsf_position`;";
		$result = self::$DB->q($sql, self::$tableName, TAB_GENERAL_SKIN_FILE_PROJECTS, TAB_PROJECTS, TAB_COUNTRIES);
		$dataReturn = array();
		while($data = $result->fetch_assoc()) {
			$dataReturn[$data['gsf_file_type']][] = $data;
		}
		return $dataReturn;
	}

    /**
     * @static
     * @param $fileId
     * @return mixed
     *  This function gives all File Projects
     */
    public static function getFileProjects($fileId) {
        return self::$DB->selectCol("SELECT `gsfp_project_id` FROM ?f WHERE `gsfp_file_id` = ?d", TAB_GENERAL_SKIN_FILE_PROJECTS, $fileId);
    }

    /**
     * @static
     * @param $fileId
     * @return bool|mysqli_result
     * This function clear File Projects
     */
     public static function clearFileProjects($fileId) {
        return self::$DB->q("DELETE FROM ?f WHERE `gsfp_file_id` = ?d", TAB_GENERAL_SKIN_FILE_PROJECTS, $fileId);
    }


    /**
     * @static
     * @param $fileId
     * @return bool|mysqli_result
     * This function delete function
     */
    public static function deleteFile($fileId) {
        return self::$DB->q("DELETE FROM ?f WHERE `id` = ?d", self::$tableName, $fileId);
    }
}
